/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mobiubiq.directory.client.object.io;

import java.util.Hashtable;
import org.json.me.JSONException;
import org.json.me.JSONObject;
import org.mobiubiq.directory.client.object.AbstractObject;
import org.mobiubiq.directory.client.object.BarcodeObject;
import org.mobiubiq.directory.client.object.LocationObject;
import org.mobiubiq.directory.client.object.TagObject;
import org.mobiubiq.directory.client.ws.Context;
import org.mobiubiq.directory.client.ws.WebServiceAction;

/**
 *
 * @author Marnix Renne
 */
public class JSONWriter {
    public String write(AbstractObject object) {
        if(object instanceof TagObject) {
            return write((TagObject)object);
        } else if(object instanceof LocationObject) {
            return write((LocationObject)object);
        } else if(object instanceof BarcodeObject) {
            return write((BarcodeObject)object);
        }
        throw new IllegalArgumentException();
    }
    
    public String write(TagObject tag) {
        try {
            JSONObject result = new JSONObject();
            result.put("id", tag.getTag());
            return escape(result.toString());
        } catch (JSONException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    public String write(LocationObject location) {
        try {
            JSONObject result = new JSONObject();
            result.put("lattitude", location.getLatitude());
            result.put("longitude", location.getLongitude());
            return escape(result.toString());
        } catch (JSONException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    public String write(BarcodeObject barcode) {
        try {
            JSONObject result = new JSONObject();
            result.put("id", barcode.getBarcode());
            result.put("code", barcode.getBarcode());
            return escape(result.toString());
        } catch (JSONException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    public String write(Context context) {
        try {
            JSONObject result = new JSONObject();
            result.put("screenWidth", context.getScreenWidth());
            result.put("screenHeight", context.getScreenHeight());
            return escape(result.toString());
        } catch (JSONException ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    public String write(WebServiceAction action) {
        try {
            JSONObject result = new JSONObject();
            result.put("name", action.getName());
            Hashtable parameters = action.getQueryParameters();
            result.put("parameters", parameters);
            return escape(result.toString());
        } catch (JSONException ex) {
            ex.printStackTrace();
            return null;
        }        
    }
    
    private String escape(String in) {
        return in;
    }
}
